﻿using Newtonsoft.Json;

namespace DiffuseEnjoyIdle.Core.Extensions
{
    public static class JsonConvertExtension
    {
        /// <summary>
        /// 反序列化为字典
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="K"></typeparam>
        /// <param name="t"></param>
        /// <param name="k"></param>
        /// <returns></returns>
        public static K ToDictionary<T, K>(this T t, K k)
        {
            return JsonConvert.DeserializeObject<K>(t.ToJson());
        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string ToJson<T>(this T t)
        {
            return JsonConvert.SerializeObject(t);
        }


        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T ToObject<T>(this string str)
        {
            return JsonConvert.DeserializeObject<T>(str);

        }
    }
}
